package jwei.apps.db;

import java.util.ArrayList;
import java.util.List;

import jwei.apps.db.ProductCache.ProductsCache;
import jwei.apps.objects.Product;
import jwei.apps.objects.Style;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class ProductDb {

    private ProductDb() {
    }

    private static final ProductDb instance = new ProductDb();

    public static ProductDb getInstance() {
        return instance;
    }

    public void addProduct(ContentResolver contentResolver, Product p) {
        if (!isCached(contentResolver, p.getProductId())) {
            ContentValues contentValue = new ContentValues();
            contentValue.put(ProductsCache.PRODUCT_ID, p.getProductId());
            contentValue.put(ProductsCache.GENDER, p.getGender());
            contentValue.put(ProductsCache.IMAGE_URL, p.getImageUrl());
            contentValue.put(ProductsCache.BRAND_ID, p.getBrandId());
            contentValue.put(ProductsCache.PRODUCT_NAME, p.getProductName());
            contentValue.put(ProductsCache.BRAND_NAME, p.getBrandName());
            contentValue.put(ProductsCache.PRODUCT_URL, p.getProductUrl());
            contentResolver.insert(ProductsCache.CONTENT_URI, contentValue);
        }
    }

    public void addProducts(ContentResolver contentResolver, List<Product> products) {
        List<ContentValues> contentValues = new ArrayList<ContentValues>();
        StyleDb sdb = StyleDb.getInstance();
        for (Product p : products) {
            if (!isCached(contentResolver, p.getProductId())) {
                ContentValues contentValue = new ContentValues();
                contentValue.put(ProductsCache.PRODUCT_ID, p.getProductId());
                contentValue.put(ProductsCache.GENDER, p.getGender());
                contentValue.put(ProductsCache.IMAGE_URL, p.getImageUrl());
                contentValue.put(ProductsCache.BRAND_ID, p.getBrandId());
                contentValue.put(ProductsCache.PRODUCT_NAME, p.getProductName());
                contentValue.put(ProductsCache.BRAND_NAME, p.getBrandName());
                contentValue.put(ProductsCache.PRODUCT_URL, p.getProductUrl());
                contentValues.add(contentValue);

                // save styles as well
                List<Style> styles = p.getStyles();
                for (Style s : styles) {
                    sdb.addStyle(contentResolver, s);
                }
            }
        }
        contentResolver.bulkInsert(ProductsCache.CONTENT_URI, contentValues.toArray(new ContentValues[0]));
    }

    public void updateProductUrl(ContentResolver contentResolver, long id, String url) {
        Uri s = ContentUris.withAppendedId(ProductsCache.CONTENT_URI, id);
        ContentValues contentValue = new ContentValues();
        contentValue.put(ProductsCache.PRODUCT_URL, url);
        contentResolver.update(s, contentValue, null, null);
    }

    public Product getProduct(ContentResolver contentResolver, long id) {
        Product ret = null;
        Uri s = ContentUris.withAppendedId(ProductsCache.CONTENT_URI, id);
        Cursor c = contentResolver.query(s, null, null, null, null);
        if (null != c && c.moveToNext()) {
            long prod_id = c.getLong(c.getColumnIndex(ProductsCache.PRODUCT_ID));
            String name = c.getString(c.getColumnIndex(ProductsCache.PRODUCT_NAME));
            String prodUrl = c.getString(c.getColumnIndex(ProductsCache.PRODUCT_URL));
            String imgUrl = c.getString(c.getColumnIndex(ProductsCache.IMAGE_URL));
            ret = new Product(prod_id, name, prodUrl, imgUrl);
            ret.setGender(c.getString(c.getColumnIndex(ProductsCache.GENDER)));
            ret.setBrandId(c.getInt(c.getColumnIndex(ProductsCache.BRAND_ID)));
            ret.setBrandName(c.getString(c.getColumnIndex(ProductsCache.BRAND_NAME)));
        }
        c.close();
        return ret;
    }

    public boolean isCached(ContentResolver contentResolver, long id) {
        boolean ret = false;
        Uri s = ContentUris.withAppendedId(ProductsCache.CONTENT_URI, id);
        Cursor c = contentResolver.query(s, null, null, null, null);
        if (c != null && c.moveToNext()) {
            ret = true;
        }
        c.close();
        return ret;
    }

    public boolean isEmpty(ContentResolver contentResolver) {
        boolean ret = true;
        String[] projection = new String[] { ProductsCache.PRODUCT_ID };
        Cursor cursor = contentResolver.query(ProductsCache.CONTENT_URI, projection, null, null, null);
        if (null != cursor && cursor.moveToNext()) {
            ret = false;
        }
        cursor.close();
        return ret;
    }

    public void refreshCache(ContentResolver contentResolver) {
        // according to doc, this is the correct way to delete all rows
        contentResolver.delete(ProductsCache.CONTENT_URI, "1", null);
    }

}
